<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑往来账': '新增往来账'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="记账类型">
          <a-select v-decorator="['types', { rules: [{ required: true, message: '请选择记账类型' }]}]" placeholder="请选择" @change="change">
            <a-select-option value="4" v-if="this.supplyTypes == 1 || this.supplyTypes == 3">供应商回款</a-select-option>
            <a-select-option value="5" v-if="this.supplyTypes == 1 || this.supplyTypes == 3">供应商退款</a-select-option>
            <a-select-option value="6" v-if="this.supplyTypes == 2 || this.supplyTypes == 3">经销商收款</a-select-option>
            <a-select-option value="7" v-if="this.supplyTypes == 2 || this.supplyTypes == 3">经销商退款</a-select-option>
            <a-select-option value="8" v-if="this.supplyTypes == 1 || this.supplyTypes == 2 || this.supplyTypes == 3">应收款</a-select-option>
            <a-select-option value="9" v-if="this.supplyTypes == 1 || this.supplyTypes == 2 || this.supplyTypes == 3">应付款</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="金额">
          <a-input-number v-decorator="['amount', { rules: [{ required: true, message: '请输入金额' }]}]" name="amount" style="width: 100%" placeholder="请输入金额" />
        </a-form-item>
        <a-form-item label="记账账户" v-if="this.typesValue !== '8' && this.typesValue !== '9'">
          <a-select v-decorator="['recordAccId', { rules: [{ required: true, message: '请选择记账账户' }]}]" placeholder="请选择" >
            <a-select-option :value="i.id" v-for="i in this.option" :key="i.value">{{ i.accName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="记账日期">
          <a-date-picker v-decorator="['date', { rules: [{ required: true, message: '请选择' }]}]" style="width: 100%" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="预计上账日期" v-if="accountType == '0'">
          <a-date-picker v-decorator="['expectDate', { rules: [{ required: true, message: '请选择' }]}]" style="width: 100%" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea rows="2" v-decorator="['memo']" placeholder="请输入备注" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
  
  <script>
  import pick from 'lodash.pick'
  import { getStAccountListUsingGET } from '@/api/api/qiantaizhangwu.ts'
  // 表单字段
  const fields = ['types','amount', 'recordAccId', 'date','expectDate', 'memo',  'id']
  
  export default {
    props: {
      visible: {
        type: Boolean,
        required: true
      },
      loading: {
        type: Boolean,
        default: () => false
      },
      supplyTypes: {
        type: String,
        default: () => false
      },
      accountType: {
        type: String,
        default: () => false
      },
      model: {
        type: Object,
        default: () => null
      }
    },
    data () {
      this.formLayout = {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 }
        }
      }
      return {
        queryParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
        typesValue:'',
        option:[],
        form: this.$form.createForm(this)
      }
    },
    created () {
      this.handleStore()
      // 防止表单未注册
      fields.forEach(v => this.form.getFieldDecorator(v))
  
      // 当 model 发生改变时，为表单设置值
      this.$watch('model', () => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    },
    methods:{
      change(e){
        this.typesValue = e
      },
      handleStore(){
        getStAccountListUsingGET(this.queryParam).then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
      },
    }
  }
  </script>